const puppeteer = require('puppeteer');

const url = 'https://movie.douban.com/tag/#/?sort=R&range=0,10&tags=电影'

const sleep = time => new Promise(resolve => {
    setTimeout(resolve, time)
})

;
(async () => {
    console.log('start visit the target page')

    const browser = await puppeteer.launch({
        args: ['--no-sandbox'],
        dumpio: false,
    })

    //  创建连接
    const page = await browser.newPage()
    await page.goto(url, {
        waitUntil: 'networkidle2'
    })

    await sleep(3000)

    await page.waitForSelector('.more')

    //  每隔三秒点击加载更多来爬取数据
    for (let i = 0; i < 1; i++) {
        await sleep(3000)
        await page.click('.more')
    }

    //  拿到想要的结果
    const result = await page.evaluate(() => {
        var $ = window.$;
        var item = $('.list-wp a');
        var links = [];
        if (item.length >= 1) {
            item.each((index, item) => {
                let it = $(item)
                let doubanId = it.find('div').data('id');
                let title = it.find('.title').text();
                let rate = Number(it.find('.rate').text())
                let poster = it.find('img').attr('src').replace('s_ratio', 'l_ratio')

                links.push({
                    doubanId,
                    title,
                    rate,
                    poster
                })
            })
        }

        return links;
    })

    //  关闭浏览器
    browser.close();
    // console.log(result)
    process.send({
        result
    })
    process.exit(0)
})()